package br.ucb.cesbdao.janelas;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;

import org.apache.log4j.Logger;

import br.ucb.cesbdao.beans.Trabalhador;
import br.ucb.cesbdao.enuns.EnumLabelAndBotao;
import br.ucb.cesbdao.enuns.EnumTipoTrabalhador;
import br.ucb.cesbdao.enuns.EnumTitulo;
import br.ucb.cesbdao.model.TrabalhadorModel;
import br.ucb.cesbdao.model.impl.TrabalhadorModelImpl;
import br.ucb.cesbdao.modeloTabelas.ModeloTabelaAtendenteApoio;
import br.ucb.cesbdao.servicos.Servicos;
import br.ucb.cesbdao.utilBeans.AtendimentoUtil;

public class SelecionaAFI extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 292372860658043220L;
	private final JPanel contentPanel = new JPanel();
	private JScrollPane spAFI;
	private JTable jtableAFI;
	private static Trabalhador atendente;
	static Logger logger = Servicos.carregaLog(SelecionaAFI.class);

	public static Trabalhador getTrabalhador() {
		return atendente;
	}
	@SuppressWarnings("static-access")
	public void setTrabalhador(Trabalhador atendente) {
		this.atendente = atendente;
	}
	ModeloTabelaAtendenteApoio modeloTabelaAFI = new ModeloTabelaAtendenteApoio();
	/**
	 * Create the dialog.
	 */
	public SelecionaAFI() {
		setModal(true);
		setTitle(EnumTitulo.TITULO_SELECIONA_AFI.getTitulo());
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);

		TrabalhadorModel model = new TrabalhadorModelImpl();
		spAFI = new JScrollPane();

		JButton btnCancelar = new JButton(EnumLabelAndBotao.CANCELAR.getTexto());
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setTrabalhador(null);
				dispose();
			}
		});
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
				gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addComponent(spAFI, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
				.addGroup(gl_contentPanel.createSequentialGroup()
						.addContainerGap(171, Short.MAX_VALUE)
						.addComponent(btnCancelar)
						.addGap(182))
				);
		gl_contentPanel.setVerticalGroup(
				gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
						.addComponent(spAFI, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(btnCancelar))
				);

		jtableAFI = new JTable();

		jtableAFI.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				setTrabalhador(modeloTabelaAFI.getTrabalhadorAt(jtableAFI.getSelectedRow()));
				dispose();
			}
		});
		ModeloTabelaAtendenteApoio.setFlag(false);
		jtableAFI.setModel(modeloTabelaAFI);
		jtableAFI.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		if(AtendimentoUtil.getFlag() != null && AtendimentoUtil.getFlag())
			modeloTabelaAFI.setTrabalhadors(model.consultaAllTrabalhadorByTipo(EnumTipoTrabalhador.AFI.getTipo()));
		else
			modeloTabelaAFI.setTrabalhadors(filtraTabelaAFIDisponiveis());
		spAFI.setViewportView(jtableAFI);
		contentPanel.setLayout(gl_contentPanel);
		jtableAFI.getColumnModel().getColumn(1).setMaxWidth(90);
		jtableAFI.getColumnModel().getColumn(1).setMinWidth(90);
	}
	public ArrayList<Trabalhador> filtraTabelaAFIDisponiveis(){
		TrabalhadorModel model = new TrabalhadorModelImpl();
		modeloTabelaAFI.setTrabalhadors(model.consultaAllTrabalhadorByTipo(EnumTipoTrabalhador.AFI.getTipo()));
		return modeloTabelaAFI.getTrabalhadorsAtendendo();
	}


}
